package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjProductPurchase;
import com.moji.entity.dto.QueryProductPurchaseDto;
import com.moji.mapper.MjProductPurchaseMapper;
import com.moji.service.MjProductPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品选购配置表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-08-13
 */
@Service
public class MjProductPurchaseServiceImpl extends ServiceImpl<MjProductPurchaseMapper, MjProductPurchase> implements MjProductPurchaseService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjProductPurchase> queryByPage(QueryProductPurchaseDto dto) {
        LambdaQueryWrapper<MjProductPurchase> wrapper = new LambdaQueryWrapper<>();
        //标题
        wrapper.like(ObjectUtil.isNotEmpty(dto.getTitle()),MjProductPurchase::getTitle
                ,dto.getTitle());
        wrapper.like(ObjectUtil.isNotEmpty(dto.getCode()),MjProductPurchase::getCode
                ,dto.getCode());
        //创建时间倒叙
        wrapper.orderByAsc(MjProductPurchase::getSort);
        wrapper.orderByDesc(MjProductPurchase::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjProductPurchase> mjProductPurchases = list(wrapper);
        PageInfo<MjProductPurchase> info = new PageInfo<>(mjProductPurchases);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjProductPurchase mjProductPurchase) {
        String code= idGenerator.idGenerator("CPXG");
        mjProductPurchase.setCode(code);
//        mjProductPurchase.setCreateId(MoJiCommonUtils.getUserCode());
//        mjProductPurchase.setUpdateId(MoJiCommonUtils.getUserCode());
        mjProductPurchase.setCreateTime(new Date());
        mjProductPurchase.setUpdateTime(new Date());
        return this.save(mjProductPurchase);
    }

    @Override
    public boolean edit(MjProductPurchase mjProductPurchase) {
        MjProductPurchase mjProductPurchase1=this.getById(mjProductPurchase.getId());
        BeanUtils.copyProperties(mjProductPurchase,mjProductPurchase1);
//        mjProductPurchase1.setUpdateId(MoJiCommonUtils.getUserCode());
        mjProductPurchase1.setUpdateTime(new Date());
        return this.updateById(mjProductPurchase1);
    }

    @Override
    public MjProductPurchase detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }
}
